var pm2    = require('pm2');
// var notify = require('./src/notify');
// var ump    = require('../src/utils/umpReport');
// var UMP = require('../utils/ump');

var processList    = [];
var watchobj       = {};
// var connectfailkey = 'wq.webmonitor.ptcxnode.marketmonitor.pm2connectfail';
// var startfailkey   = 'wq.webmonitor.ptcxnode.marketmonitor.pm2startfail';

pm2.connect(function (err) {
    console.log('connect')
    if (err) {
        // bussReport(connectfailkey);
        // UMP.addValue(UMP.key.pm2Connect);
        console.error(err);
        process.exit(2);
    }

    pm2.start({
        name              : 'webstatic',
        cwd               : '../', //工作目录
        script            : './startup.sh', //启动脚本
        "exec_interpreter": "bash",
        exec_mode: 'fork', // cluster模式
        instances         : 1, //0,启动最大数量 (cluster mode)
        max_memory_restart: '300M', // Optional: Restarts your app if it reaches 100Mo
        merge_logs        : true,//合并日志
        log_date_format   : "YYYY-MM-DD HH:mm Z",//日志格式
        log_file          : "./webstatic-pm2-log/combined.outerr.log",
        out_file          : "./webstatic-pm2-log/out.log",
        error_file        : "./webstatic-pm2-log/err.log",
        min_uptime        : 30000,//应用运行少于时间被认为是异常启动；
        max_restarts      : 50,  //最大异常重启次数，即小于min_uptime运行时间重启次数；
        restart_delay     : 5000,//time to wait before restarting a crashed app (in milliseconds). defaults to 0.
        env_dev           : {
            //args    : ["--ignore-watch=\.git|webstatic-monitor|forever|log|public|node_modules|views|uploads|polling|userfiles"],
            NODE_ENV: "development"
        },
        env               : {
            //args    : ["--ignore-watch=\.git|webstatic-monitor|forever|log|public|node_modules|views|uploads|polling|userfiles"],
            NODE_ENV: "production"
        },
        ignore_watch      : ['**/.*', './webstatic-monitor', './forever', './log', './logs', './public', './node_modules', './views', './uploads', './polling', './userfiles', './webstatic-pm2-log', './seclog', 'test.js', './test', './temp'],
        //watch             : ['./bin', './config', './routes', './src', './views', 'app.js'],
        watch             : true,
        watch_options     : {
            followSymlinks: false
        }
    }, function (err, apps) {
        if (err) {
            // UMP.addValue(UMP.key.pm2Start);

            // bussReport(startfailkey)
            throw err
        } else {
            pm2.list(function (err, processDescriptionList) {
                if (err) {
                    // UMP.addValue(UMP.key.pm2List);

                    throw err
                } else {
                    processList = processDescriptionList;
                    if (processList.length) {
                        processList.forEach(function (ceil, index) {
                            if (!watchobj[ceil.pid]) {
                                watchobj[ceil.pid] = ceil;
                            }
                        })
                    }
                }
                // notify.launchBus();
                //pm2.disconnect(); // Disconnects from PM2
            })
        }
    });
});

function bussReport(key) {
    // var umpclient = ump.bussReport.register(key);
    // umpclient.addValueReport(1);
}